package edgeEntity

import (
	"encoding/json"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
)

type OperateEntity struct {
	BaseEntity *commEntity.BaseEntity // 基本信息

	Manufacturer string // 设备厂商名称
	DeviceType   string // 设备类型名

	OperateName string // 操作命令
	OperateMode string // 操作模式
	DataType    string // 返回的数据类型
	EngineType  string // 引擎类型
	ServiceType string // 业务类型
	Timeout     int    // 通信超时
	Polling     bool   // 该操作是否需要被轮询调度

	EngineParam map[string]interface{} // 配置参数
	ExtendParam map[string]interface{} // 扩展参数
}

type operatePo struct {
	Id           int64                  `json:"id" db:"id"`                     // 对象id
	CreateTime   int64                  `json:"createTime" db:"create_time"`    // 创建时间
	UpdateTime   int64                  `json:"updateTime" db:"update_time"`    // 更新时间
	Manufacturer string                 `json:"manufacturer" db:"manufacturer"` // 设备厂商名称
	DeviceType   string                 `json:"deviceType" db:"device_type"`    // 设备类型名
	OperateName  string                 `json:"operateName" db:"operate_name"`  // 操作命令
	OperateMode  string                 `json:"operateMode" db:"operate_mode"`  // 操作模式
	DataType     string                 `json:"dataType" db:"data_type"`        // 返回的数据类型
	EngineType   string                 `json:"engineType" db:"engine_type"`    // 引擎类型
	ServiceType  string                 `json:"serviceType" db:"service_type"`  // 业务类型
	Timeout      int                    `json:"timeout" db:"timeout"`           // 通信超时
	Polling      bool                   `json:"polling" db:"polling"`           // 该操作是否需要被轮询调度
	EngineParam  map[string]interface{} `json:"engineParam" db:"engine_param"`  // 配置参数
	ExtendParam  map[string]interface{} `json:"extendParam" db:"extend_param"`  // 扩展参数
}

func (e *OperateEntity) Type() string {
	return "OperateEntity"
}

func (e *OperateEntity) TableName() string {
	return "tb_operate"
}

func (e *OperateEntity) GetBaseEntity() *commEntity.BaseEntity {
	return e.BaseEntity
}

func (e *OperateEntity) Instance() *OperateEntity {
	return &OperateEntity{
		BaseEntity:  new(commEntity.BaseEntity),
		Timeout:     0,
		Polling:     false,
		EngineParam: make(map[string]interface{}),
		ExtendParam: make(map[string]interface{}),
	}
}

func (e *OperateEntity) NewEntity() commEntity.IEntity {
	return e.Instance()
}

func (e *OperateEntity) NewPo() interface{} {
	return &operatePo{
		EngineParam: make(map[string]interface{}),
		ExtendParam: make(map[string]interface{}),
	}
}

func (e *OperateEntity) MakeServiceKey() string {
	p := e.MakeServiceKeyList()
	return commEntity.MakeServiceKey(p)
}

func (e *OperateEntity) MakeServiceKeyList() []string {
	var p []string
	p = append(p, e.Manufacturer)
	p = append(p, e.DeviceType)
	p = append(p, e.OperateName)
	return p
}

func (e *OperateEntity) BuildByJson(jsn *string) error {
	var po operatePo
	err := json.Unmarshal([]byte(*jsn), &po)
	if err != nil {
		return err
	}

	e.BuildByPo(&po)
	return nil
}

func (e *OperateEntity) BuildToJson() (string, error) {
	po := e.BuildToPo()
	code, err := json.Marshal(po)
	if err != nil {
		return "", err
	}

	return string(code), nil
}

func (e *OperateEntity) BuildByPo(pop interface{}) {
	ptr := e
	po := pop.(*operatePo)

	ptr.BaseEntity.Id = po.Id
	ptr.BaseEntity.CreateTime = po.CreateTime
	ptr.BaseEntity.UpdateTime = po.UpdateTime
	ptr.EngineParam = po.EngineParam
	ptr.ExtendParam = po.ExtendParam
	ptr.Manufacturer = po.Manufacturer
	ptr.DeviceType = po.DeviceType
	ptr.OperateName = po.OperateName
	ptr.OperateMode = po.OperateMode
	ptr.DataType = po.DataType
	ptr.EngineType = po.EngineType
	ptr.ServiceType = po.ServiceType
	ptr.Timeout = po.Timeout
	ptr.Polling = po.Polling
}

func (e *OperateEntity) BuildToPo() interface{} {
	ptr := e
	po := operatePo{}

	po.Id = ptr.BaseEntity.Id
	po.CreateTime = ptr.BaseEntity.CreateTime
	po.UpdateTime = ptr.BaseEntity.UpdateTime
	po.EngineParam = ptr.EngineParam
	po.ExtendParam = ptr.ExtendParam
	po.Manufacturer = ptr.Manufacturer
	po.DeviceType = ptr.DeviceType
	po.OperateName = ptr.OperateName
	po.OperateMode = ptr.OperateMode
	po.DataType = ptr.DataType
	po.EngineType = ptr.EngineType
	po.ServiceType = ptr.ServiceType
	po.Timeout = ptr.Timeout
	po.Polling = ptr.Polling

	return &po
}

func (e *OperateEntity) BindEntity(other *OperateMethodEntity) {
	ptr := e

	ptr.Manufacturer = other.Manufacturer
	ptr.DeviceType = other.DeviceType
	ptr.OperateName = other.OperateName
	ptr.EngineType = other.EngineType
	ptr.OperateMode = other.OperateMode
	ptr.ServiceType = other.ServiceType
	ptr.DataType = other.DataType
	ptr.Timeout = other.Timeout
	ptr.Polling = other.Polling

	ptr.BaseEntity.Id = other.BaseEntity.Id
	ptr.BaseEntity.CreateTime = other.BaseEntity.CreateTime
	ptr.BaseEntity.UpdateTime = other.BaseEntity.UpdateTime
}
